<?php
	function addcart($uid,$shop_id,$sku_id,$num,$type,$ctuan_id,$buygood){
		$shop=M('shop');
		$cart=M('cart');
		$shop_sku=M('shop_sku');
		$store=M('store');
//		if($recommend_id==''||$uid==$recommend_id){
//			$recommend_id=null;
//		}
		$shop_info=$shop->find($shop_id);
		$store_info=$store->find($shop_info['store_id']);
		
		if($type==1){
			$cart->where('buyer_id='.$uid.' and tuanselect='.$type)->delete();
		
			if($ctuan_id){
				$order=M('order');
				$info=$order->find($ctuan_id);
				if($info['buyer_id']==$uid){
					//不允许参加自己团
					return 2;
				}
				$info_user=$order->where("buyer_id=$uid and tuan_is_first_id=$ctuan_id and order_state>'10'")->select();
				if($info_user){
					//不允许参加自己团
					return 5;
				}
			}
		}
	
		
		$type = isset($type) ? $type : 0;
		if($sku_id!=0){
			$shop_sku_info=$shop_sku->where('sku_id='.$sku_id)->find();
			//判断购物车是否有
			$rs =$cart->where('shop_id='.$shop_id.' and sku_id='.$sku_id.' and tuanselect='.$type.' and buyer_id='.$uid)->find();	
		}else{
			$rs =$cart->where('shop_id='.$shop_id.' and tuanselect='.$type.' and buyer_id='.$uid)->find();
		}
		//检测是否能购买
		if($rs){
			if($buygood==1){
				$gm_num=$num;
			}else{
				$gm_num=$rs['num']+$num;
			}
			//$gm_num=$rs['num']+$num;
		}else{
			$gm_num=$num;
		}
		//1检测库存
		if($sku_id!=0){
			$shop_sku_info=$shop_sku->where('sku_id='.$sku_id)->find();
			if($gm_num>$shop_sku_info['sku_stock']){
				return 3;
			}
		}else{
			if($gm_num>$shop_info['num']){
				return 3;
			}
		}
		//检测起拍量
		if($shop_info['qp_num']>0){
			if($gm_num<$shop_info['qp_num']){
				return 6;
			}
		}
		//2检测限购
		if($shop_info['xz_num']>0){
			$shop_list=M('order_goods')
					->field('yt_order_goods.goods_num')
					->join('yt_order ON yt_order.order_id = yt_order_goods.order_id')
					->where('yt_order.order_state>10 and yt_order.refund_state==0')
					->select();
			$ygm_num=0;
			if($shop_list){
				foreach($shop_list as $key=>$value){
					$ygm_num=$ygm_num+$value['goods_num'];
				}
			}
			$ygm_num=$ygm_num+$gm_num;	
			if($ygm_num>$shop_info['xz_num']){
				return 4;
			}
		}
        if($rs&&($buygood==0)){
            $add_c=$cart->where('cart_id='.$rs['cart_id'])->setInc('num',$num);
            //$add_c=$cart->where('cart_id='.$rs['cart_id'])->setInc('num',$num);
            session('cart_list_id',$rs['cart_id']);
        }else{
            $data['buyer_id']=$uid;
            $data['shop_id']=$shop_info['id'];
            $data['shop_name']=$shop_info['title'];
            $data['store_id']=$store_info['id'];
            $data['store_name']=$store_info['title'];
            $data['mian_send']=$shop_info['mian_send'];
            $data['sku_id']=$sku_id;
            if($sku_id!=0){

                if($type==1){
                    if($shop_sku_info['sku_price_tuan']){
                        $data['price']=$shop_sku_info['sku_price_tuan'];
                    }else{
                        $data['price']=$shop_info['tuan_price'];
                    }

                }else{
                    $data['price']=$shop_sku_info['sku_price'];
                }
            }else{
                if($type==1){
                    $data['price']=$shop_info['tuan_price'];
                }else{
                    $data['price']=$shop_info['price'];
                }
            }
            $data['num']=$num;
            $data['shop_picture']=$shop_info['image'];
            $data['tuanselect']=$type;
            $data['tuan_id']=$ctuan_id;
            //$data['recommend_id']=$recommend_id;
            $add_c=$cart->add($data);
            session('cart_list_id',$add_c);
        }
		/*if($rs){
			if($buygood==1){
				if($rs['num']!=$num){
					$new_num['num']=$num;
					$add_c=$cart->where('cart_id='.$rs['cart_id'])->save($new_num);
				}else{
					$add_c=true;
				}
				
			}else{
				$add_c=$cart->where('cart_id='.$rs['cart_id'])->setInc('num',$num);
			}
			//$add_c=$cart->where('cart_id='.$rs['cart_id'])->setInc('num',$num);
			session('cart_list_id',$rs['cart_id']);
		}else{
			$data['buyer_id']=$uid;
			$data['shop_id']=$shop_info['id'];
			$data['shop_name']=$shop_info['title'];
			$data['store_id']=$store_info['id'];
			$data['store_name']=$store_info['title'];
            $data['mian_send']=$shop_info['mian_send'];
			$data['sku_id']=$sku_id;
            if($sku_id!=0){

                if($type==1){
                    if($shop_sku_info['sku_price_tuan']){
                        $data['price']=$shop_sku_info['sku_price_tuan'];
                    }else{
                        $data['price']=$shop_info['tuan_price'];
                    }

                }else{
                    $data['price']=$shop_sku_info['sku_price'];
                }
            }else{
                if($type==1){
                    $data['price']=$shop_info['tuan_price'];
                }else{
                    $data['price']=$shop_info['price'];
                }
            }
			$data['num']=$num;
			$data['shop_picture']=$shop_info['image'];
			$data['tuanselect']=$type;
			$data['tuan_id']=$ctuan_id;
			//$data['recommend_id']=$recommend_id;
			$add_c=$cart->add($data);
			session('cart_list_id',$add_c);
		}*/
		if($add_c){
			return 1;
		}else{
			return 0;
		}
		
	}
	//获取店铺信息
	function get_store($store_id){
		$store=M('store');
		$store_info=$store->find($store_id);
		return $store_info;
	}
	
	//获取店铺所属分类
	function get_store_class($type){
		$type=explode(',',$type);
		$store_classification=M('store_classification');
		
		$html='';
		foreach($type as $key=>$value){
			$type_info=$store_classification->find($value);
		
			if($key==0){
				$html.=$type_info['classification'];
			}else{
				$html.=','.$type_info['classification'];
			}
		}
		return $html;
	}
	
	//获取用户地址信息
	function get_user_address($user_id){
		$users_address=M('users_address');
		$users_address=$users_address->where('user_id='.$user_id.' and is_default=1')->find();
		return $users_address;
	}
	
	function get_moblie_address(){
		$moblie_address=F('moblie_address');
		if(!$moblie_address){
			$rs=M('sys_province')
			->join('yt_sys_city ON yt_sys_city.province_id = yt_sys_province.province_id','left')
			->join('yt_sys_district ON yt_sys_district.city_id = yt_sys_city.city_id','left')
			->select();
			foreach($rs as $key=>$value){
				$newarray[$value['province_id']][n]=$value['province_name'];
				$newarray[$value['province_id']][c][$value['city_id']][n]=$value['city_name'];
				$newarray[$value['province_id']][c][$value['city_id']][a][]=$value['district_name'];
			}
//			foreach($newarray as $k1=>$v1){
//				foreach($v1[c] as $k2=>$v2){
//					print_r($v2[c]);
//					$newarray[$k1][c][$k2][c]=array_values($v2[c]);
//					print_r(array_values($v2[c]));
//					
//				}
//			}
//			print_r($newarray);
//			die;
			F('moblie_address',$newarray);
			
			$moblie_address=$newarray;
		}
		
		return json_encode($moblie_address);
	}
	
	
	//获取用户
	function get_user_info($user_id){
		$users=M('users');
		$users_info=$users->find($user_id);
		return $users_info;
	}
	//获取商品信息
	function get_shop_info($shop_id){
		$shop=M('shop');
		$shop_info=$shop->find($shop_id);
		return $shop_info;
	}
	//获取发票配置
	function get_web_invoice(){
		$web_qita = F("web_qita");
		if(empty($web_qita)){
			$options_obj = M("Options");
			$option = $options_obj->where("option_name='web_qita'")->find();
	
			if($option){
				$web_qita = json_decode($option['option_value'],true);
			}else{
				$web_qita = array();
			}
			F("web_qita", $web_qita);
		}
		return $web_qita;	
	}
	//获取订单商品
	function get_order_shop($order_id){
		$order_goods=M('order_goods');
		$order_goods_list=$order_goods->where('order_id='.$order_id)->select();
		return $order_goods_list;
	}
	//获取订单总价格
	function get_count_price($order_cn){
		$order=M('order');
		$result=$order->where('order_cn='.$order_cn)->select();
		if($result){
			foreach($result as $key=>$value){
    			$time=$value['add_time'];
    			if($value['coupon']!=null){
	    				$youhui_price=get_coupon_price($value['buyer_id'],$value['coupon']);
	    		}
	    		$count_price=$count_price+$value['order_amount']-$value['daiyan_sale']-$youhui_price;
	    	}
		}else{
			$result=$order->where('order_sn='.$order_cn)->find();
			$youhui_price=get_coupon_price($result['buyer_id'],$result['coupon']);
    		$count_price=$result['order_amount']-$result['daiyan_sale']-$youhui_price;
		}
		return $count_price;
	}
	//获取订单状态
	function get_order_status($order_cn,$status){
		$flag=true;
		$order=M('order');
		$result=$order->where('order_cn='.$order_cn)->select();
		if($result){
			foreach($result as $key=>$value){
    			if($value['order_state']!=$status){
    				$flag=false;
    			}
	    	}
		}else{
			$result=$order->where('order_sn='.$order_cn)->find();
    		if($result['order_state']!=$status){
    			$flag=false;	
    		}
		}
		return $flag;
	}
	//添加未支付订单到redis
	function addredis($out_trade_no){
		$order=M('order');
		$result=$order->where('order_cn='.$out_trade_no)->select();
		$newdata=array();
		if($result){
			foreach($result as $key=>$value){
				$data['order_id']=$value['order_id'];
				$data['order']=$value['order_sn'];
				$data['add_time']=$value['add_time'];
				if($value['tuanselect']==1){
					$data['clostime']=60*3;
				}else{
					$data['clostime']=60*60*24;
				}
				$newdata[]=$data;
	    	}
		}else{
			$result=$order->where('order_sn='.$out_trade_no)->find();
    		$data['order_id']=$result['order_id'];
			$data['order']=$result['order_sn'];
			$data['add_time']=$result['add_time'];
			if($result['tuanselect']==1){
				$data['clostime']=60*3;
			}else{
				$data['clostime']=60*60*24;
			}
			$newdata=$data;
		}
		$redis =connectRedis();
		$order_list = $redis->get('order_list');
		$order_list=json_decode($order_list,true);
		$order_list = array_merge($order_list, $newdata); 
		$order_list=json_encode($order_list);
		$redis->set('order_list',$order_list);
	}//支付更新订单状态
	function update_order_status($out_trade_no,$trade_no,$status,$payway){
		$order=M('order');
		$flag=true;
		$result=$order->where('order_cn='.$out_trade_no)->select();
        file_put_contents("order.txt", date("Y-m-d H:i:s") . "out_trade_no   " . print_r($out_trade_no, true) . "\r\n", FILE_APPEND);
        file_put_contents("order.txt", date("Y-m-d H:i:s") . "result   " . print_r($result, true) . "\r\n", FILE_APPEND);

        if($result){
            $order_add_time=$result[0]['add_time'];
			foreach($result as $key=>$value){
				$data['order_id']=$value['order_id'];
				$data['pay_sn']=$trade_no;
                //免发货
                if($value['mian_send']==1&&$status==20){

                    //判断拼团是否成功
                    if($value['tuanselect']==1){
                        if($value['tuan_is_first_id']==0){
                            $num=get_tuan($value['order_id']);
                            $tuan_m_send['order_id']=$value['order_id'];
                        }else{
                            $num=get_tuan($value['tuan_is_first_id']);
                            $tuan_m_send['order_id']=$value['tuan_is_first_id'];
                        }
                        if($num==$value['tuannum']){
                            $data['order_state']='30';
                            $data['shipping_time']=time();
                            $tuan_m_send['status']=1;
                        }else{
                            $data['order_state']=$status;
                            $tuan_m_send['status']=0;
                        }
                    }else{
                        $data['order_state']='30';
                        $data['shipping_time']=time();
                    }
                }else{
                    $data['order_state']=$status;
                }

                $data['payment_code']=$payway;
				$data['payment_time']=time();
				$rs=$order->save($data);
				$uid=$value['buyer_id'];
				
				//减库存
				if($status==20){
					$good_list=M('order_goods')->field('goods_id,goods_num,goods_pay_price,goods_sku,goods_sku_spec,goods_sku_style')->where('order_id='.$value['order_id'])->select();
					foreach($good_list as $k=>$v){
						if($v['goods_sku']==0){
							M('shop')->where('id='.$v['goods_id'])->setDec('num',$v['goods_num']);
						}else{
							M('shop_sku')->where('sku_id='.$v['goods_sku'])->setDec('sku_stock',$v['goods_num']);
							$sku_shop_info=M('shop')->find($v['goods_id']);
							$sku=json_decode($sku_shop_info['shop_sku'],true);
							$from=$sku['SKU_Form'];
							foreach($from as $k2=>$v2){
								if($v['goods_sku_spec']==$v2['sku_spec']&&$v['goods_sku_style']==$v2['sku_style']){
									$sku['SKU_Form'][$k2]['sku_stock']=$v2['sku_stock']-$v['goods_num'];
								}
							}
							$update_sku['shop_sku']=json_encode($sku);
							$update_sku['id']=$v['goods_id'];
							M('shop')->save($update_sku);
						}
						

                        //增加销量
                        M('shop')->where('id='.$v['goods_id'])->setInc('sale_num',$v['goods_num']);
                        //增加众筹销量和金额
                        M('crowdfunding')->where('goods_id='.$v['goods_id'])->setInc('num',$v['goods_num']);
                        M('crowdfunding')->where('goods_id='.$v['goods_id'])->setInc('now_money',$v['goods_pay_price']);
                      
                    }
				}
				//判断拼团是否成功
				if($value['tuanselect']==1){
					if($value['tuan_is_first_id']==0){
						$num=get_tuan($value['order_id']);
					}else{
						$num=get_tuan($value['tuan_is_first_id']);
					}
					if($num==$value['tuannum']){
						$newdata['tuan_is_success']=1;
						//拼团成功时的订单肯定是子订单
						$order->where('tuan_is_first_id='.$value['tuan_is_first_id'])->save($newdata);
						$order->where('order_id='.$value['tuan_is_first_id'])->save($newdata);
					}
				}
				if(!$rs){
					$flag=false;
				}
                //用户余额扣除
				$memberInfo=M('member')->find($value['member_id']);
				$member_data['id']=$value['member_id'];
                $member_data['money']=$memberInfo['money']-$value['balance_use'];
                M('member')->save($member_data);
	    	}
		}else{
			$result=$order->where('order_sn='.$out_trade_no)->find();
			$order_add_time=$result['add_time'];
    		$data['order_id']=$result['order_id'];
			$data['pay_sn']=$trade_no;
            //免发货
            if($result['mian_send']==1&&$status==20){
                //判断拼团是否成功
                if($result['tuanselect']==1){
                    if($result['tuan_is_first_id']==0){
                        $num=get_tuan($result['order_id']);
                        $tuan_m_send['order_id']=$result['order_id'];
                    }else{
                        $num=get_tuan($result['tuan_is_first_id']);
                        $tuan_m_send['order_id']=$result['tuan_is_first_id'];
                    }
                    if($num==$result['tuannum']){
                        $data['order_state']='30';
                        $data['shipping_time']=time();

                        $tuan_m_send['status']=1;
                    }else{
                        $data['order_state']=$status;
                        $tuan_m_send['status']=0;
                    }
                }else{
                    $data['order_state']='30';
                    $data['shipping_time']=time();
                }

            }else{
                $data['order_state']=$status;
            }
			$data['payment_code']=$payway;
			$data['payment_time']=time();
			$rs=$order->save($data);
			$uid=$result['buyer_id'];
			//减库存
			if($status==20){
				$good_list=M('order_goods')->field('goods_id,goods_num,goods_pay_price,goods_sku,goods_sku_spec,goods_sku_style')->where('order_id='.$result['order_id'])->select();
				foreach($good_list as $k=>$v){
					if($v['goods_sku']==0){
						M('shop')->where('id='.$v['goods_id'])->setDec('num',$v['goods_num']);
					}else{
						M('shop_sku')->where('sku_id='.$v['goods_sku'])->setDec('sku_stock',$v['goods_num']);
						$sku_shop_info=M('shop')->find($result['goods_id']);
						$sku=json_decode($sku_shop_info['shop_sku'],true);
						$from=$sku['SKU_Form'];
						foreach($from as $k2=>$v2){
							if($v['goods_sku_spec']==$v2['sku_spec']&&$v['goods_sku_style']==$v2['sku_style']){
								$sku['SKU_Form'][$k2]['sku_stock']=$v2['sku_stock']-$v['goods_num'];
							}
						}
						$update_sku['shop_sku']=json_encode($sku);
						$update_sku['id']=$v['goods_id'];
						M('shop')->save($update_sku);
					}
             
					//增加销量
					M('shop')->where('id='.$v['goods_id'])->setInc('sale_num',$v['goods_num']);
					//增加众筹销量和金额
                    M('crowdfunding')->where('goods_id='.$v['goods_id'])->setInc('num',$v['goods_num']);
                 	M('crowdfunding')->where('goods_id='.$v['goods_id'])->setInc('now_money',$v['goods_pay_price']);
                  
                }
			}
			//判断拼团是否成功
			if($result['tuanselect']==1){
				if($result['tuan_is_first_id']==0){
					$num=get_tuan($result['order_id']);
				}else{
					$num=get_tuan($result['tuan_is_first_id']);
				}
				if($num==$result['tuannum']){
					$newdata['tuan_is_success']=1;
					//拼团成功时的订单肯定是子订单
					$order->where('tuan_is_first_id='.$result['tuan_is_first_id'])->save($newdata);
					$order->where('order_id='.$result['tuan_is_first_id'])->save($newdata);
				}
			}
			if(!$rs){
				$flag=false;
			}
            $memberInfo=M('member')->find($result['member_id']);
            $member_data['id']=$result['member_id'];
            $member_data['money']=$memberInfo['money']-$result['balance_use'];
            M('member')->save($member_data);
		}
        //团购免发货
        if($flag&&$tuan_m_send['status']==1){
            $m_data['order_state']='30';
            $m_data['shipping_time']=time();
            $order->where('tuan_is_first_id='.$tuan_m_send['order_id'])->save($m_data);
            $order->where('order_id='.$tuan_m_send['order_id'])->save($m_data);
        }
		//支付成功通知
		if($flag&&$status==20){
			$user_info=M('users')->find($uid);
			if($user_info['wx_h5openid']){
				$openid=$user_info['wx_h5openid'];
				$url='https://m.58daiyan.com/user/order_info/order/'.$out_trade_no;
				$keyword1=$out_trade_no;
				$keyword2=date('Y年m月d日  H:i',time());
				$keyword3=get_count_price($out_trade_no);
				$keyword4=$payway;
				send_info($openid,$url,$keyword1,$keyword2,$keyword3,$keyword4);
			}
			//检查获得红包通知
			$result=$order->where('order_cn='.$out_trade_no)->select();
			if($result){
				foreach($result as $key=>$value){
					$shop_list=M('order_goods')->where("order_id=".$value['order_id'])->select();
					foreach($shop_list as $k=>$v){
						if($v['recommend_id']){
							$rec_user=M('users')->find($v['recommend_id']);
							if($rec_user['wx_h5openid']){
								if($v['recommend_type']==0){
									$rec_shop=M('shop')->find($v['goods_id']);
									$openid=$rec_user['wx_h5openid'];
									$url='https://m.58daiyan.com/user/daiyan.html';
									$keyword1=$rec_shop['redenvelope'].'元红包';
									$keyword2=date('Y年m月d日  H:i',$order_add_time);
									send_info_hongbao($openid,$url,$keyword1,$keyword2);
								}
								if($v['recommend_type']==1){
									$rec_shop=M('shop')->find($v['recommend_shop_id']);
									$openid=$rec_user['wx_h5openid'];
									$url='https://m.58daiyan.com/user/daiyan.html';
									$keyword1=$rec_shop['title'];
									$keyword2=date('Y年m月d日  H:i',$order_add_time);
									send_info_hongbao($openid,$url,$keyword1,$keyword2);
								}
							}
							
						}
					}
				}
			}else{
				$result=$order->where('order_sn='.$out_trade_no)->find();
				$shop_list=M('order_goods')->where("order_id=".$result['order_id'])->select();
				foreach($shop_list as $k=>$v){
						if($v['recommend_id']){
							$rec_user=M('users')->find($v['recommend_id']);
							if($rec_user['wx_h5openid']){
								if($v['recommend_type']==0){
									$rec_shop=M('shop')->find($v['goods_id']);
									$openid=$rec_user['wx_h5openid'];
									$url='https://m.58daiyan.com/user/daiyan.html';
									$keyword1=$rec_shop['redenvelope'].'元红包';
									$keyword2=date('Y年m月d日  H:i',$order_add_time);
									send_info_hongbao($openid,$url,$keyword1,$keyword2);
								}
								if($v['recommend_type']==1){
									$rec_shop=M('shop')->find($v['recommend_shop_id']);
									$openid=$rec_user['wx_h5openid'];
									$url='https://m.58daiyan.com/user/daiyan.html';
									$keyword1=$rec_shop['title'];
									$keyword2=date('Y年m月d日  H:i',$order_add_time);
									send_info_hongbao($openid,$url,$keyword1,$keyword2);
								}
							}
							
						}
					}
				
			}
			
		}
      	
		//收货代言通知
		if($flag&&$status==40){
			
			$user_info=M('users')->find($uid);
		
			if($user_info['wx_h5openid']){
				$openid=$user_info['wx_h5openid'];
				$url='https://m.58daiyan.com/user/daiyan.html';
				$keyword1=date('Y年m月d日  H:i',$order_add_time);
				$keyword2=$out_trade_no;
				send_info_daiyan($openid,$url,$keyword1,$keyword2);
			}
			
		}
		return $flag;
	}
	
	//更新商品的评论数、平均评分
	
	function aver_shop($shop_id){
		$shop=M('shop');
		$order_goods_evaluate=M('order_goods_evaluate');
		$avgScore = $order_goods_evaluate->where("goods_id=$shop_id")->avg('scores');
		$count = $order_goods_evaluate->where("goods_id=$shop_id")->count();
		$data['id']=$shop_id;
		$data['evaluation_good_star']=$avgScore;
		$data['evaluation_count']=$count;
		$shop->save($data);
	}
	
//	//获取优惠券金额
//	function get_coupon_price($user_id,$coupon_id){
//		$coupon_type=M('coupon_type');
//		$coupon=M('coupon');
//		$result=$coupon->where("coupon_type_id=$coupon_id and uid=$user_id and state=1")->select();
//		if($result){
//			$rs=$coupon_type->find($coupon_id);
//		}else{
//			$rs['money']=0;
//		}
//		return $rs['money'];
//	}
	//获取物流公司
	function get_express(){
		$express=M('express');
		$rs=$express->where("is_enabled=1")->order('sort desc')->select();
		return	$rs;
	}
	
	function get_coupon_user($user_id,$coupon_id){
		$coupon=M('coupon');
		$rs=$coupon->where("coupon_type_id=$coupon_id and uid=$user_id")->select();
        $coupon_info=M('coupon_type')->FIND($coupon_id);
        $data['num']=count($rs);
        $data['max_fetch']=$coupon_info['max_fetch'];
		return	$data;
	}

/**
 * 计算剩余天时分。
 * $unixEndTime string 终止日期的Unix时间
 * @author tangxinzhuan
 * @version 2016-10-28
 */
function ShengYu_Tian_Shi_Fen($unixEndTime=0)
{
    if ($unixEndTime <= time()) { // 如果过了活动终止日期
        return '0天0时0分';
    }

    // 使用当前日期时间到活动截至日期时间的毫秒数来计算剩余天时分
    $time = $unixEndTime - time();

    $days = 0;
    if ($time >= 86400) { // 如果大于1天
        $days = (int)($time / 86400);
        $time = $time % 86400; // 计算天后剩余的毫秒数
    }

    $xiaoshi = 0;
    if ($time >= 3600) { // 如果大于1小时
        $xiaoshi = (int)($time / 3600);
        $time = $time % 3600; // 计算小时后剩余的毫秒数
    }

    $fen = (int)($time / 60); // 剩下的毫秒数都算作分

    return $days.'天'.$xiaoshi.'时'.$fen.'分';
}

function weizhi($user_id,$province,$city,$district){
    $province='%'.$province.'%';
    $city='%'.$city.'%';
    $district='%'.$district.'%';
    $province_map['province_name'] = array('like',$province);
    $sys_province=M('sys_province');
    $rs_province=$sys_province->where($province_map)->find();
    $province_id=$rs_province['province_id'];

    $city_map['city_name'] = array('like',$city);
	$city_map['province_id']=$province_id;
    $sys_city=M('sys_city');
    $rs_city=$sys_city->where($city_map)->find();
    $city_id=$rs_city['city_id'];

    $district_map['district_name'] = array('like',$district);
	$district_map['city_id'] = $city_id;
    $sys_district=M('sys_district');
    $rs_district=$sys_district->where($district_map)->find();
    $district_id=$rs_district['district_id'];
    if($province_id&&$city_id&&$district_id){
        $info['province_id']=$province_id;
        $info['city_id']=$city_id;
        $info['district_id']=$district_id;
        $info['id']=$user_id;
        M('users')->save($info);
    }else{
        file_put_contents("diqu.txt", $province.$city.$district."\r\n", FILE_APPEND);
    }
}
?>